package com.cyss.greenball.dao;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lianzt.commondata.AbstractCommonData;
import com.lianzt.commondata.DataConvertFactory;
import com.lianzt.exception.InstanceDataException;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SaveMsgDao {
	private final static String DATABASE_NAME = "greenball.db";

	public static void putValue(Context context, String key,
			AbstractCommonData acd) {
		DatabaseHelper dh = new DatabaseHelper(context, DATABASE_NAME);
		SQLiteDatabase sdb = dh.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("key", key);
		cv.put("value", DataConvertFactory.praseString(acd).getBytes());
		if (getValue(context, key) == null) {
			sdb.insert("savemsg", null, cv);
		} else {
			sdb.update("savemsg", cv, "key=?", new String[] { key });
		}
		sdb.close();
	}

	public static AbstractCommonData getValue(Context context, String key) {
		DatabaseHelper dh = new DatabaseHelper(context, DATABASE_NAME);
		SQLiteDatabase sdb = dh.getReadableDatabase();
		AbstractCommonData acd = null;
		Cursor cursor = sdb.rawQuery("select value from savemsg where key='"
				+ key + "'", null);
		if (cursor.moveToNext()) {
			try {
				acd = DataConvertFactory.getInstance(new String(cursor
						.getBlob(0), "UTF-8"));
			} catch (InstanceDataException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		cursor.close();
		sdb.close();
		return acd;
	}

	public static void removeValue(Context context, String key) {
		DatabaseHelper dh = new DatabaseHelper(context, DATABASE_NAME);
		SQLiteDatabase sdb = dh.getWritableDatabase();
		sdb.execSQL("delete from savemsg where key='" + key + "'");
		sdb.close();
	}

	public static void showKeyList(Context context) {
		DatabaseHelper dh = new DatabaseHelper(context, DATABASE_NAME);
		SQLiteDatabase sdb = dh.getReadableDatabase();
		Cursor cursor = sdb.rawQuery("select key from savemsg", null);
		if (cursor.moveToNext()) {
			System.out.println(cursor.getString(0));
		}
		sdb.close();
	}

	public static List<String> getKeyList(Context context) {
		List<String> keyList = new ArrayList<String>();
		DatabaseHelper dh = new DatabaseHelper(context, DATABASE_NAME);
		SQLiteDatabase sdb = dh.getReadableDatabase();
		Cursor cursor = sdb.rawQuery("select key from savemsg", null);
		if (cursor.moveToNext()) {
			keyList.add(cursor.getString(0));
		}
		sdb.close();
		return keyList;
	}

	public static List<AbstractCommonData> getAllValue(Context context) {
		List<AbstractCommonData> l_acd = new ArrayList<AbstractCommonData>();
		DatabaseHelper dh = new DatabaseHelper(context, DATABASE_NAME);
		SQLiteDatabase sdb = dh.getReadableDatabase();
		Cursor cursor = sdb.rawQuery("select value from savemsg", null);
		while (cursor.moveToNext()) {
			try {
				AbstractCommonData acd = DataConvertFactory
						.getInstance(new String(cursor.getBlob(0)));
				l_acd.add(acd);
			} catch (InstanceDataException e) {
				e.printStackTrace();
			}
		}
		sdb.close();
		return l_acd;
	}

	public static Map<String, AbstractCommonData> getAllValueWithKey(
			Context context) {
		Map<String, AbstractCommonData> m_acd = new HashMap<String, AbstractCommonData>();
		DatabaseHelper dh = new DatabaseHelper(context, DATABASE_NAME);
		SQLiteDatabase sdb = dh.getReadableDatabase();
		Cursor cursor = sdb.rawQuery("select key,value from savemsg", null);
		while (cursor.moveToNext()) {
			try {
				AbstractCommonData acd = DataConvertFactory
						.getInstance(new String(cursor.getBlob(1)));
				m_acd.put(cursor.getString(0), acd);
			} catch (InstanceDataException e) {
				e.printStackTrace();
			}
		}
		sdb.close();
		return m_acd;
	}
}
